package com.yuanfengPlus.basicInfoManage.AddressLibrary.service.impl;

import com.yuanfengPlus.basicInfoManage.AddressLibrary.dao.AddressLibraryDao;
import com.yuanfengPlus.basicInfoManage.AddressLibrary.model.AddressLibrary;
import com.yuanfengPlus.basicInfoManage.AddressLibrary.service.IAddressLibraryService;
import com.yuanfengPlus.utils.RedisCache;
import com.yuanfengPlus.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IAddressLibraryServiceImpl implements IAddressLibraryService {

    @Autowired
    private AddressLibraryDao addressLibraryDao;

    @Resource
    private RedisCache redisCache;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result addAddressLibrary(AddressLibrary addressLibrary) {
        // 步骤1：通过行级锁查询，防止并发修改冲突
        AddressLibrary existingAddressLibrary = null;
        try {
            existingAddressLibrary = addressLibraryDao.selectForUpdate(addressLibrary.getAddressLibId());
        } catch (Exception e) {
            return Result.failure(500, "获取锁失败，可能发生了死锁或资源争用，请稍后重试。");
        }
        if (existingAddressLibrary != null) {
            return Result.failure(400, "该地址库信息已存在，请勿重复添加。");
        }
        // 步骤2：执行新增操作
        int i = addressLibraryDao.insertAddress(addressLibrary);
        if (i == 1) {
            // 步骤3：删除当前redis中的地址库信息，防止数据不一致
            redisCache.deleteObject("addressLibrary");
            // 查询所有的地址库信息并重新录入redis
            List<AddressLibrary> addressLibrarys = addressLibraryDao.queryAddressLibrary(addressLibrary);
            redisCache.setCacheList("addressLibrary", addressLibrarys);
            redisCache.expire("addressLibrary", 24 * 60 * 60);
            return Result.success(addressLibrary, "新增地址库信息成功。");
        } else {
            return Result.failure(400, "新增地址库信息失败。");
        }
    }
    @Override
    public Result queryAddressLibrary(AddressLibrary addressLibrary, Integer pageNum, Integer pageSize) {
        // 查询redis中有没有地址库信息
        List<Object> addressLibraryInRedis = redisCache.getCacheList("addressLibrary");
        if (addressLibraryInRedis.size() != 0) {
            System.out.println("从redis中获取到地址库信息。");
            return Result.success(addressLibraryInRedis);
        } else {
            List<AddressLibrary> addressLibraries = addressLibraryDao.queryAddressLibrary(addressLibrary, pageNum, pageSize);
            if (addressLibraries.size() != 0) {
                // 将查询到的信息存入redis并设置过期时间为24小时
                System.out.println("从数据库中获取到地址库信息。");
                redisCache.setCacheList("addressLibrary", addressLibraries);
                redisCache.expire("addressLibrary", 24 * 60 * 60);
                System.out.println("将地址库信息存入redis。");
                return Result.success(addressLibraries);
            } else {
                return Result.failure(400, "当前地址库内没有信息。");
            }
        }
    }
}
